#!/bin/bash
# This script checks for all required drive mounts, GDrive token validity, and rclone/mergerfs service status. If any issues are found, it shuts down any containers (media-service.txt) dependent on these mounted drives to avoid any data issues and sends an email notification. 
# A mail transfer agent such as nullmailer, ssmtp, or postfix must be configured correctly for emails to work

# CHECKING FOR DRIVE MOUNTS

rclone_drive="/media/drive"
rclone_crypt="/media/crypt"
hdd1="/media/hdd1"
merge_media="/media/hdd1/data/media"
num_drives=4

mounted=0
notmounted=""

if mount | grep ${rclone_drive} > /dev/null; then
	mounted=$((mounted+1))
#	echo -e ${rclone_drive}' mounted.'
else 
#	echo ${rclone_drive}' not mounted.'
	notmounted="${notmounted} ${rclone_drive}\n"
fi

if mount | grep ${rclone_crypt} > /dev/null; then
	mounted=$((mounted+1))
#	echo -e ${rclone_crypt}' mounted.'
else 
#	echo -e ${rclone_crypt}' not mounted.'
	notmounted="${notmounted} ${rclone_crypt}\n"
fi

if mount | grep ${hdd1} > /dev/null; then
	mounted=$((mounted+1))
#	echo -e ${hdd1}' mounted.'
else 
#	echo -e ${hdd1}' not mounted.'
	notmounted="${notmounted} ${hdd1}\n"
fi

if mount | grep ${merge_media} > /dev/null; then
	mounted=$((mounted+1))
#	echo -e ${merge_media}' mounted.'
else 
#	echo -e ${merge_media}' not mounted.'
	notmounted="${notmounted} ${merge_media}\n"
fi

#echo $mounted
#echo -e ${notmounted}

if [[ "$mounted" -ne "$num_drives" ]]; then
	echo -e "These drives are not mounted properly:\n\n${notmounted}\n\nShutting down media containers." | mail -s "[DVM ERROR] Some drives not mounted" info@example.com -aFrom:DockerVM@example.com
	sudo docker stop $(cat /home/USER/docker/scripts/media-services.txt)
#else
	#echo "All drives mounted." | mail -s "All drives mounted." info@example.com -aFrom:DockerVM@example.com
fi

######## CHECKING FOR EXPIRED TOKENS

TOKEN_ERROR="expired or revoked"
RCLONE_LOGS="/home/USER/docker/appdata/rclone/logs/*.log"
logfiles="$(egrep -l "$TOKEN_ERROR" $RCLONE_LOGS)"

if [[ ! -z "$logfiles" ]]; then
	echo -e "These rclone log files have token expired/revoked error:\n\n${logfiles}\n\nShutting down media containers." | mail -s "[DVM ERROR] Token expired or revoked" info@example.com -aFrom:DockerVM@example.com
	sudo docker stop $(cat /home/USER/docker/scripts/media-services.txt)
#else
	#echo "Rclone token still appears to be valid." | mail -s "No error in Rclone logs." info@example.com -aFrom:DockerVM@example.com
fi

######## CHECKING RCLONE and MERGERFS SERVICE ERRORS

# MergerFS
merge_media_status="$(systemctl is-active merge-media.service)"
if [[ ! "$merge_media_status" == "active" ]]; then
	echo -e "merge-media.service is reporting an issue:\n\n${merge_media_status}\n\nShutting down media containers." | mail -s "[DVM ERROR] merge-media.service not running" info@example.com -aFrom:DockerVM@example.com
	sudo docker stop $(cat /home/USER/docker/scripts/media-services.txt)
#else
#    echo "Active"
fi

# Rclone Google Drive
rclone_drive_status="$(systemctl is-active rclone-drive.service)"
if [[ ! "$rclone_drive_status" == "active" ]]; then
	echo -e "rclone-drive.service is reporting an issue:\n\n${rclone_drive_status}\n\nShutting down media containers." | mail -s "[DVM ERROR] rclone-drive.service not running" info@example.com -aFrom:DockerVM@example.com
	sudo docker stop $(cat /home/USER/docker/scripts/media-services.txt)
#else
#    echo "Active"
fi

# Rclone Google Drive Encrypted
rclone_crypt_status="$(systemctl is-active rclone-crypt.service)"
if [[ ! "$rclone_crypt_status" == "active" ]]; then
	echo -e "rclone-crypt.service is reporting an issue:\n\n${rclone_crypt_status}\n\nShutting down media containers." | mail -s "[DVM ERROR] rclone-crypt.service not running" info@example.com -aFrom:DockerVM@example.com
	sudo docker stop $(cat /home/USER/docker/scripts/media-services.txt)
#else
#    echo "Active"
fi
